package com.cytech.cloudclinic.moudle.main.clinic.admission;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.cytech.cloudclinic.R;
import com.cytech.cloudclinic.base.BaseActivity;
import com.cytech.cloudclinic.base.BasePresenter;
import com.cytech.cloudclinic.bean.Patient;
import com.cytech.cloudclinic.moudle.main.clinic.admission.intelligent.IntelligentFragment;
import com.cytech.cloudclinic.moudle.main.patient.detail.PatientDetailActivity;
import com.cytech.cloudclinic.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 接诊
 */
public class AdmissionActivity extends BaseActivity {

    @BindView(R.id.titlebar_back)
    ImageView titlebarBack;
    @BindView(R.id.admission_main_container)
    FrameLayout container;
    @BindView(R.id.rg_admission_manager)
    RadioGroup rgAdmissionManager;

    @BindView(R.id.tv_prescription_list_name)
    TextView tvPatientName;
    @BindView(R.id.tv_prescription_list_sex)
    TextView tvPatientSex;
    @BindView(R.id.tv_prescription_list_age)
    TextView tvPatientAge;

    private int currentFragmentIndex = 0;
    private List<Fragment> fragmentList;
    private Fragment currentFragment;
    private Fragment fragment1;
    private Fragment fragment2;
    private Fragment fragment3;
    private Fragment fragment4;
    private Fragment fragment5;
    private Fragment fragment6;

    private Patient patient;


    public static void actionStart(Context context, Patient patient) {
        Intent intent = new Intent(context, AdmissionActivity.class);
        intent.putExtra("patient",patient);
        context.startActivity(intent);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.act_clinic_admission;
    }

    @Override
    protected void initView() {
        patient = (Patient) getIntent().getSerializableExtra("patient");
        int patientId = patient.patient_id;
        tvPatientName.setText(patient.name);
        tvPatientSex.setText(patient.getGender());
        if(!TextUtils.isEmpty(patient.birthday)){
            tvPatientAge.setText(TimeUtils.getAgeDetail(patient.birthday));
        }

        fragmentList = new ArrayList<>();
        fragment1 = AdmissionStyleFragment.newInstance(10,patientId,patient);
        fragment2 = AdmissionStyleFragment.newInstance(4,patientId,patient);
        fragment3 = AdmissionStyleFragment.newInstance(2,patientId,patient);
        fragment4 = AdmissionStyleFragment.newInstance(3,patientId,patient);
        fragment5 = AdmissionStyleFragment.newInstance(5,patientId,patient);
        fragment6 = AdmissionStyleFragment.newInstance(1,patientId,patient);

        fragmentList.add(fragment1);
        fragmentList.add(fragment2);
        fragmentList.add(fragment3);
        fragmentList.add(fragment4);
        fragmentList.add(fragment5);
        fragmentList.add(fragment6);



        switchFragment(0);

        rgAdmissionManager.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_admission_1:
                        switchFragment(0);
                        break;
                    case R.id.rb_admission_2:
                        switchFragment(1);
                        break;
                    case R.id.rb_admission_3:
                        switchFragment(2);
                        break;
                    case R.id.rb_admission_4:
                        switchFragment(3);
                        break;
                    case R.id.rb_admission_5:
                        switchFragment(4);
                        break;
                    case R.id.rb_admission_6:
                        switchFragment(5);
                        break;
                }
            }
        });
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.titlebar_back)
    public void exit(View v) {
        finish();
    }

    private void switchFragment(int position) {
        currentFragmentIndex = position;
        Fragment targetFragment = fragmentList.get(position);
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        if (!targetFragment.isAdded()) {
            if (currentFragment != null) {
                transaction.hide(currentFragment);
            }
            transaction.add(R.id.admission_main_container, targetFragment).commit();
            //System.out.println("还没添加呢");
        } else {
            transaction
                    .hide(currentFragment)
                    .show(targetFragment)
                    .commit();
            //System.out.println("添加了( ⊙o⊙ )哇");
        }
        currentFragment = targetFragment;
    }

    @OnClick(R.id.iv_prescription_patient_infop)
    public void OnPatientInfoClick(View view){
        PatientDetailActivity.actionStart(this,patient.patient_id,patient.name);
    }
}
